<?php

namespace App\Services;
use App\Models\Tag;
use App\Models\Video;
use App\Models\VideoTag;
use Illuminate\Support\Facades\DB;
use PHPUnit\Exception;
use QL\QueryList;

Class CollectService extends BaseService {

    const domain = 'https://www.rbw31.com/';
    public $map = ['ts'=>'categories/ry/'];

    public function categoty($category)  //根据分类采集
    {
//        $arr = [1,2,3,4,5,6,7];
//        $num =0;
//        foreach ($arr as $value){
//            $num++;
//            echo $num;
//        }
//        die;

        try{
            for($i=1;$i<=91;$i++){
                $ip = $this->ip();
                $ua = $this->getua();
                $uaRandNum = rand(0,(count($ua['pc'])-1));
                $num =0;
                $client = new \GuzzleHttp\Client();
                $res = $client->request('GET', self::domain.$this->map[$category].'?from='.$i, [
                    'timeout' => 60,
                ]);
                $html = (string)$res->getBody();
                $data = QueryList::html($html);
                $rt = $data->find('.item>a')->attrs('href');
                foreach ($rt as $video){
                    $num++;
                    if($num>=4){
                        $num=0;
                        $ip = $this->ip();
                        $ua = $this->getua();
                        $uaRandNum = rand(0,(count($ua['pc'])-1));
                    }
                    $res = $client->request('GET', $video, [
                        'timeout' => 60,
                        'headers' => [
//                'Accept-Encoding' => 'gzip, deflate, br',
                            'Accept'=>'*/*',
                            'Host'=>'www.rbw31.com',
                            'Connection'=>'keep-alive',
                            'X-FORWARDED-FOR'=>$ip,
                            'Origin'=>'https://www.rbw31.com',
                            'upgrade-insecure-requests'=>1,
                            'User-Agent'=>$ua['pc'][$uaRandNum],
                        ]
                    ]);
                    $html = (string)$res->getBody();

                    $data = QueryList::html($html);
                    $link = $data->find('source')->attr('src');
                    $title = $data->find('h1')->text();
                    $img = $data->find('meta:eq(5)')->attr('content');
                    $time = $data->find('.item>span:eq(0)>em')->text(); //时常
                    $browse = trim($data->find('.item>span:eq(1)>em')->text());   //浏览量
                    $tags = explode(',',$data->find('meta:eq(2)')->attr('content'));
                    DB::beginTransaction();

//            echo $link.PHP_EOL;
//            echo $title.PHP_EOL;
//            echo $img.PHP_EOL;
//            echo $time.PHP_EOL;
//            echo $browse.PHP_EOL;
                    $res = $client->request('GET', $link, [
                        'timeout' => 60,
                        'headers' => [
//                'Accept-Encoding' => 'gzip, deflate, br',
                            'Accept'=>'*/*',
                            'Host'=>'www.rbw31.com',
                            'Connection'=>'keep-alive',
                            'X-FORWARDED-FOR'=>$ip,
                            'Origin'=>'https://www.rbw31.com',
                            'upgrade-insecure-requests'=>1,
                            'User-Agent'=>$ua['pc'][$uaRandNum],
                        ]
                    ]);
                    $link = '/ts/'.time().'.m3u8';
                    file_put_contents(public_path().$link,$res->getBody()->getContents());
                    $video = Video::create([
                        'title'=>$title,
                        'time' =>$time,
                        'img' => $img,
                        'browse' => $browse,
                        'link' => $link,
                        'category_id' =>1,  //写死ts
                    ]);

                    foreach ($tags as $tag){
                        if(!$tagModel = Tag::where(['title'=>$tag])->first()){
                            $tagModel = Tag::create(['title'=>$tag]);
                        }
                        VideoTag::create(['video_id'=>$video->id,'tag_id'=>$tagModel->id]);
                    }
                    DB::commit();
                    echo '成功'.PHP_EOL;
                }
            }

        }catch (Exception $exception){
            DB::rollBack();
        }

    }

    public function getUa(){
        return [
            'pc' => [
                "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
                "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0",
                "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.3; rv:11.0) like Gecko",
                "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0",
                "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
                "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
                "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
                "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)",
                "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
            ],
            'mobile'=>[
                "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
                "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
                "Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
                "Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
                "MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
                "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
                "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13",
                "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",
                "Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0",
                "NOKIA5700/ UCWEB7.0.2.37/28/999",
                "Openwave/ UCWEB7.0.2.37/28/999",
                "Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/999",
                "Mozilla/5.0 (Linux; Android 6.0; 1503-M02 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.2 TBS/036558 Safari/537.36 MicroMessenger/6.3.25.861 NetType/WIFI Language/zh_CN"
            ]
        ];
    }
    public function ip()
    {
        $ip_long = array(
            array('607649792', '608174079'), // 36.56.0.0-36.63.255.255
            array('1038614528', '1039007743'), // 61.232.0.0-61.237.255.255
            array('1783627776', '1784676351'), // 106.80.0.0-106.95.255.255
            array('2035023872', '2035154943'), // 121.76.0.0-121.77.255.255
            array('2078801920', '2079064063'), // 123.232.0.0-123.235.255.255
            array('-1950089216', '-1948778497'), // 139.196.0.0-139.215.255.255
            array('-1425539072', '-1425014785'), // 171.8.0.0-171.15.255.255
            array('-1236271104', '-1235419137'), // 182.80.0.0-182.92.255.255
            array('-770113536', '-768606209'), // 210.25.0.0-210.47.255.255
            array('-569376768', '-564133889'), // 222.16.0.0-222.95.255.255
        );
        $rand_key = mt_rand(0, 9);
        return $ip = long2ip(mt_rand($ip_long[$rand_key][0], $ip_long[$rand_key][1]));
    }

}
